/*************************************************************************
Purpose:
          This program analyzes the grace accel (stack) timeseries.
************************************************************************** 
Input:    None, but does open a real input file to get times,lats,x,y,z.
**************************************************************************
Copyright 2010 Bryan Killett

This file is part of GAIA.

GAIA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GAIA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GAIA. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
//Header file "definitions.hpp" declares many functions, constants, datatypes.
//In order to use the functions declared in definitions.hpp, must link
//with object code that includes functions.cpp.
#include "definitions.hpp"

int main(int argc, char *argv[]){

  input_s input;
  string basefolder(gaiafolder());
  string file;
  string input_file(gaiafolder());//Filename of options file created in IDL.
  string input_folder(gaiafolder());//Folder where options/gsi files are found.
  ancillary_s ancillary;
  grid_s region;
  grid_s grid;
  model_s model;
  results_s results, regional_results;
  plot_options_s plot_options, temp_options;//Temp used for region code
  insitu_s insitu;//For tide gauge data, etc.
  //These variables are used to display run time of program after execution.
  time_t start_time,end_time;
  timespan_conversion_s timespan_conversion;
  string base;//This string forms the base part of the output filename.
  
  //The next variables are used to load data from cache or save to cache.
  string foldername, current_foldername,true_foldername,filename,newfile;

  //Used to create output folder from command line arguments.
  string temp_string;
  FILE *output;
  string temp_string2;

  start_time = time(NULL);//Record time at start of program.

  input_folder.append("c/");
  input_file = input_folder; input_file.append("options.txt");
  load_options(input_file, input, region.options, ancillary, grid, model.options, results, plot_options, insitu, 1);

  //Use current time and command line args to create output folder.
  create_output_folder(argc,argv,plot_options);

  //Copy options.txt to newly-created folder.
  temp_string2 = "cp ";
  temp_string2.append(gaiafolder());
  temp_string2.append("c/options.txt ");
  temp_string2.append("\"");
  temp_string2.append(plot_options.outputfolder.c_str());
  temp_string2.append("\"");
  //cout<<"options.txt copy string: "<<temp_string2<<endl;
  output = popen(temp_string2.c_str(),"r");
  pclose(output);

  //Each program should manually set this value.
  //See definitions.hpp for more details.
  results.options.output_choice = 2;

  //input.options.filename = "stack_and_orbit_land_and_ocean_edited.txt";
  //input.options.filename = "stack_7_yrs_20s_smoothing.txt";
  //input.options.filename = "stack_and_orbit_sim_fes_big10.txt";
  //input.options.filename = "stack_and_orbit_sim_type_72.txt";
  //input.options.filename = "stack_and_orbit_sim_type_72_supp3.txt";
  //input.options.filename = "stack_and_orbit_sim_type_72_diff_supp0n3.txt";
  //195- FES M2,K1, diff inf and deg90, 350km, new areas
  //196- FES M2,K1, diff inf and deg90, 230km, new areas
  //197- FES M2,K1, diff inf and deg90, 150km, new areas
  //198- FES M2,K1, diff inf and deg90, 100km, new areas
  //199- FES M2,K1, diff inf and deg90, 50km, new areas
  input.options.filename = "stack_and_orbit_sim_type_199_supp0.txt";
  //input.options.filename   = "stack_and_orbit_sim_type_116_supp0.txt";
  //input.options.filename   = "stack_and_orbit_gaia_global_region2.txt";
  //input.options.filename   = "stack_and_orbit_sima_global_region.txt";
  //input.options.filename   = "stack_and_orbit_gaia_minus_sima.txt";
  //input.options.filename   = "stack_and_orbit_hudson_swaths.txt";
  //input.options.filename   = "stack_and_orbit_sim_type_114_supp0.txt";
  //input.options.filename   = "stack_and_orbit_99_minus_111_and_107.txt";
  //input.options.filename = "stack_and_orbit_sim_type_72_CRN.txt";
  //input.options.filename = "stack_and_orbit_deg90_minus_deg150_2008_20s_smoothing.txt";
  //input.options.filename="stack_and_orbit_short2.txt";//150000 pts.
  //input.options.filename="stack_deg150_2008_20s_smoothing_50N.txt";

  //Load ancillary data (topography, temperatures, etc) from disk.
  load_ancillary(ancillary, 1);

  //load_input() expects a grid to already be defined.
  define_grid(grid, ancillary, results);

  region.options.type = 0;
  region = define_region(region.options, grid, 1);

  //Older input files do not contain orbit data.  If loading one of these
  //files (identifiable because the filename doesn't contain "_and_orbit",
  //set this variable to 1.  Otherwise it's 0.
  //If this filename contains "region_", it needs to be read.
  if(((int)input.options.filename.find("_and_orbit"))!=(int)string::npos) input.options.no_orbit_data = 0;
  else input.options.no_orbit_data = 1;

  load_input(input,grid,region);

  //Time domain.
  //   1 - Plots the first N swath timeseries in different windows (hardcoded)
  //   2 - Plots histogram of accels bw +/- regparam1 nm/s^2 with #bins = nmax
  
  //Spectral domain.
  //1000 - Tests FT power spectra by simulating many signals.
  //1001 - Plots the first N FT power spectra in diff windows (hardcoded)
  //1010 - Averages some properties of the first N FT power spectra.
  
  //2000 - Tests hi and low pass filter by sending it simulations from 1000.
  results.options.type=1;
  results.options.cutoff_freq=0.01;

  results.options.nmax = 100;
  results.options.regparam1 = 100;
  
  perform_timeseries_analysis(input, results);

//Set the output filename base.
  base = "plot_data";
  
  //Now that the analysis has been performed, it's time to output the data
  //in a format that the IDL program grace_input_map.pro can turn into a 
  //plot.  Last variable is 1 because this is the final write_output.
  write_output(base,results, grid, plot_options,1);

  end_time = time(NULL);//Record time at end of program.
  timespan_conversion.seconds = (long)(end_time - start_time);
  cout<<"grace_stack_timeseries finished after "<<sec2human(timespan_conversion)<<"."<<endl;

  return 0;
}
